package org.apache.flink.connector.jdbc.databases.mysql.dialect;

import java.util.Arrays;
import java.util.stream.Collectors;

import static java.lang.String.format;

/**
 * @program: flink-connector-jdbc-3.1.1
 * @description: ${description}
 * @author: Copyright(c)2022-2032 carlos All Rights Reserved
 * @create by xiaolong.guo on 2023/10/20 17:57
 */
public class testDialect_ts {

    public static void main(String[] args) {
        String tableName = "books";
        String[] selectFields = new String[]{"id,title,authors,year"};
        String[] conditionFields = new String[]{"id,title"};
        String select = getSelectFromStatement(tableName, selectFields, conditionFields);
        
        System.out.print("结果：" + select + "\n");
        
    }
    public static String getSelectFromStatement(
            String tableName, String[] selectFields, String[] conditionFields) {

        String selectExpressions =
                Arrays.stream(selectFields)
                        .map(f -> quoteIdentifier(f))   // .map(s::quoteIdentifier) 
                        .collect(Collectors.joining(", "));
        System.out.print("查询字段：" + selectExpressions  + "\n");

        String fieldExpressions =
                Arrays.stream(conditionFields)
                        .map(f -> format("%s = :%s", quoteIdentifier(f), f))
                        .collect(Collectors.joining(" AND "));
        System.out.print("查询条件"  + fieldExpressions  + "\n");

        
        return "SELECT "
                + selectExpressions
                + " FROM "
                + quoteIdentifier(tableName)
                + (conditionFields.length > 0 ? " WHERE " + fieldExpressions : "");
    }

    public static String quoteIdentifier(String identifier) {
        return "`" + identifier + "`";
    }
}
